// pages/my/my.js
import { throttle } from '~/utils/util.js';
import baseuser from '~/utils/users.js'
import basesup from '~/utils/supplier.js'
import baseteam from '~/utils/teamleader.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        kefucode:'',
        jsons: [
            { id: 1 },
            { id: 2 },
            { id: 3 },
            { id: 4 },
            { id: 5 },
            { id: 6 },
            { id: 7 },
        ],
        yangpinnumb: {},
        dianpunumb: [0, 0, 0],
        show: false,
        userInfo:{}
    },
    async getcode() {
        let resdata = await baseuser.getcde()
        console.log(resdata)
        this.setData({
            kefucode: resdata.data.item.code
        })
    },
    navesetupsupp() {
        wx.navigateTo({
            url: '../setup_supp/setup_supp',
        })
    },
    navecoll() {
        wx.navigateTo({
            url: '../addresssubscription_coll/subscription_coll',
        })
    },
    async getsample() {
        let reqdata = {
            userId: this.data.userInfo.id
        }
        let fivedata = await baseteam.samplemanag(reqdata)
        if (fivedata.message == "操作成功") {
            this.setData({
                yangpinnumb: fivedata.data.result
            })
        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
        }
    },
    // 店铺数据
    async getsherenumb() {
        let reqdata = {
            userId: this.data.userInfo.id
        }
        let sherenumb = await basesup.supnumb(reqdata)
        console.log(sherenumb)
        if (sherenumb.data.data.message == "查询成功") {
            this.setData({
                dianpunumb: sherenumb.data.data.items
            })
        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
        }
    },
    async getsupplines() {
        let resdata = await basesup.ourderstate()
        console.log(resdata)
        if (resdata.code == 200) {
            this.setData({
                yangpinnumb: resdata.data.result
            })
        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
        }

    },
    navesamplestate(item) {
        console.log(item)
        wx.navigateTo({
            // url: `../../pages/samplestate/samplestate?state=${item.currentTarget.dataset.orderstate}`,
            url: `/my/samplestate/samplestate?state=${item.currentTarget.dataset.orderstate}`,

        })
    },
    navedetail(item) {
        wx.navigateTo({
            url: `../../pages/orderdetail/orderdetail?state=${item.currentTarget.dataset.orderstate}`,
        })
    },
    navecommodity() {
        wx.navigateTo({
            url: '../../pages/commodity_admin/commodity_admin',
        })
    },
    navecommodityex() {
        wx.navigateTo({
            url: '../../pages/commodity_examine/commodity_examine',
        })
    },
    naveaddcomm() {
        wx.navigateTo({
            url: '../../pages/addproduct/addproduct',
        })
    },
    navesubscription() {
        wx.navigateTo({
            url: '../../pages/addresssubscription_coll/subscription_coll',
        })
    },
    navetimeauthentica() {
        wx.navigateTo({
            url: '../../pages/authentica/timeauthentica',
        })
    },
    navemyhomepage() {
        wx.navigateTo({
            url: `/my/myhomepages/myhomepages?id=${this.data.userInfo.id}`,

        })
    },
    nevedatacenter() {
        wx.navigateTo({
            url: '../../pages/datacenter/datacenter',
        })
    },
    navesetup() {
        wx.navigateTo({
            url: '../../pages/setup/setup',
        })
    },
    gouserindex() {
        wx.navigateTo({
            url: `../../pages/shophomepage/shophomepage?id=${this.data.userInfo.storeId}`,
        })
    },
    navebusinesscard() {
        wx.navigateTo({
            url: '../../pages/businesscard/businesscard',
        })
    },
    navecompany() {
        wx.navigateTo({
            url: '../../pages/authentica/companyauthentica',
        })
    },
    navelogins() {
        let keys = wx.getStorageSync("usermsg")
        if (keys) {
            console.log(keys)
            wx.navigateTo({
                // url: '../../pages/logins/logins',
                url: '/pages/setup_supp/setup_supp',
    
            })
        } else{
            wx.navigateTo({
                // url: '../../pages/logins/logins',
                url: '/logins/logins/logins',
            })
        }
 
    },
    jiaohuan(item) {
        let aa = this.data.jsons
        var temp = aa.splice(item.currentTarget.dataset.lined, 1);
        aa.splice(item.currentTarget.dataset.lined - 1, 0, temp[0]);
        console.log(aa)
    },
    fanhui() {
        // let fa = baseapi.fanhui()
    },
    ceshi1: throttle(async () => {
        let josndata = {
            sdk: "000"
        }
        // let ceshi1 = await expceshi3(josndata)
        // let ceshi2 = await expceshi5(josndata)

        // console.log(ceshi1)
        let aaex = await baseapi.expceshi3(josndata)
        console.log(aaex)
        console.log("a")
    }, 3000, 2000),

    async ceshi2() {
        // setTimeout(function () {
        //     console.log("ceshi2 -2000")
        // }, 2000);
        // setTimeout(function () {
        //     console.log("ceshi2 -3000")
        // }, 3000);

        new Promise((resolve) => {
            setTimeout(() => {
                console.log("First setTimeout");
                resolve();
            }, 2000);
        }).then(() => {
            new Promise((resolve) => {
                setTimeout(() => {
                    console.log("Second setTimeout");
                    resolve();
                }, 5000);
            });
        });


    },
    // 更新用户信息
    async gattimecomst() {
        let logins = await baseuser.timecompanystart()
        let that = this
        if (logins.code == 200 && logins.data.data.message == "查询成功") {
            // if (that.data.userInfo.identity != logins.data.data.item.identity) {
            wx.getStorageSync({
                key: "usermsg",
                data: logins.data.data.item
            })
            that.setData({
                userInfo: logins.data.data.item,
                hasUserInfo: true,
            })
            if (logins.data.data.item.identity == 1) {
                that.getsample()
            }
            if (logins.data.data.item.identity == 2) {
                that.getsupplines()
                that.getsherenumb()
            }
            // } else {
            //     if (that.data.userInfo.identity == 1) {
            //         that.getsample()
            //     }
            //     if (that.data.userInfo.identity == 2) {
            //         that.getsupplines()
            //         that.getsherenumb()
            //     }
            // }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getcode()
        let that = this
        let statusBarHeight = (0, wx.getSystemInfoSync)().statusBarHeight;
        that.setData({
            height: 46 + statusBarHeight,
        });
        wx.getStorage({
            key: 'usermsg',
            success(res) {
                that.setData({
                    userInfo: res.data,
                    hasUserInfo: true,
                })
                that.gattimecomst()
                // if (res.data.identity == 0) {
                //     // that.gattimecomst()
                // }
                // if (res.data.identity == 1) {
                //     that.getsample()
                // }
                // if (res.data.identity == 2) {
                //     that.getsupplines()
                //     that.getsherenumb()
                // }
            }
        })

    },

    lianxikefu() {
        this.setData({
            show: true
        })
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
     },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this
        console.log(wx.getStorageSync("usermsg"))
        if (wx.getStorageSync("usermsg")!=''|| wx.getStorageSync("usermsg")!=null) {
            console.log(wx.getStorageSync("usermsg"))
            that.setData({
                userInfo: wx.getStorageSync("usermsg")
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})